package com.banshou.installs.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.banshou.installs.R;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.StaffManagerBinding;
import com.banshou.installs.model.dao.RequestDelStaff;
import com.banshou.installs.model.dao.ResponseLogin;
import com.banshou.installs.model.dao.ResponseStaff;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.adapter.StaffManAdapter;
import com.banshou.installs.utils.DialogUtils;
import com.banshou.installs.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;

/**
 * Created by cjq on 2018/1/2.
 * Email: stoic_yb@139.com
 * features:
 */

public class StaffManagerActivity extends BaseActivity implements View.OnClickListener {
    private StaffManagerBinding mBinding;
    private DialogUtils dialogUtils;
    private int delPosition;

    private List<ResponseStaff.Employee> mDatas;
    private StaffManAdapter mAdapter;
    private Dialog dialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.staff_manager);
        initView();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        dialogUtils.showAni(this);
        getEmploy();
    }

    private void getEmploy() {
        RetrofitManager2.getInstance()
                .create(ResponseInfo.class)
                .loadEmployee()
                .compose(RxUtil.lifecycle(this))
                .subscribe(new Observer<ResponseStaff>() {
                    @Override
                    protected void onNextStep(ResponseStaff responseStaff) {
                        dialogUtils.hideAni();
                        if (responseStaff.getEmpList() != null) {
                            mDatas.clear();
                            mDatas.addAll(responseStaff.getEmpList());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.toast("没有人员信息");
                        }
                    }

                    @Override
                    protected void onErrorResult() {
                        dialogUtils.hideAni();
                    }
                });
    }

    private void initView() {
        mDatas = new ArrayList<>();
        dialogUtils = new DialogUtils();
        mAdapter = new StaffManAdapter(this, R.layout.staff_manager_item, mDatas);
        mBinding.recycleview.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.recycleview.setAdapter(mAdapter);
        mBinding.title.pagerTitle.setText(R.string.self_workcon);
        mBinding.title.rightTitle.setText(R.string.self_addstaff);
        mBinding.title.rightTitle.setVisibility(View.VISIBLE);
        mBinding.title.rightTitle.setOnClickListener(this);
        mBinding.title.back.setOnClickListener(this);
        dialogUtils.showAni(StaffManagerActivity.this);
        getEmploy();
        del();
    }

    /**
     * 删除回调
     */
    public void del() {
        mAdapter.setOnItemClickListener(position -> {
            delPosition = position;
            dialog = new Dialog(StaffManagerActivity.this, R.style.edit_AlertDialog_style);
            View view = LayoutInflater.from(StaffManagerActivity.this).inflate(R.layout.del_dialog, null);
            dialog.setContentView(view);
            dialog.show();
            Button confirm = view.findViewById(R.id.confirm);
            Button cancel = view.findViewById(R.id.cancel);
            confirm.setOnClickListener(StaffManagerActivity.this);
            cancel.setOnClickListener(StaffManagerActivity.this);
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.right_title:
                Intent intent = new Intent(this, AddStaffActivity.class);
                startActivity(intent);
                break;
            case R.id.cancel:
                if (dialog != null) {
                    dialog.cancel();
                }
                break;
            case R.id.confirm:
                if (dialog != null) {
                    dialog.cancel();
                }
                RequestDelStaff delStaff = new RequestDelStaff(mDatas.get(delPosition).getId());
                RetrofitManager2.getInstance()
                        .create(ResponseInfo.class)
                        .delStaff(delStaff)
                        .compose(RxUtil.lifecycle(this))
                        .subscribe(new Observer<ResponseLogin>() {
                            @Override
                            protected void onNextStep(ResponseLogin ResponseLogin) {
                                mDatas.remove(delPosition);
                                mAdapter.notifyDataSetChanged();
                                ToastUtil.toast(Config.DELSUCC);
                            }

                            @Override
                            protected void onErrorResult() {

                            }
                        });
                break;
        }
    }
}
